import config from './config';
import log from './log'
import cache from './cache'
import http from './http'
import helper from './helper';
import dialog from './plugin/dialog/index'
import msg from './plugin/msg/index'

let globalStore = null;
let globalConfig = null;
let appLog = null;
let appCache = null;
let appHttp = null;
let constant = {};
export default function install(app, injectKey) {
    globalConfig = config();
    globalConfig.setAppConfig(injectKey.config);
    globalStore = injectKey.useStore;
    constant = injectKey.constant;
    appLog = log(config);
    appCache = cache(config);
    appHttp = new http(config);

    app.config.globalProperties.$log = appLog;
    app.config.globalProperties.$cache = appCache;
    app.config.globalProperties.$http = appHttp;
    app.config.globalProperties.$dialog = dialog;
    app.config.globalProperties.$msg = msg;

    for (let key in helper) {
        app.config.globalProperties[`$${key}`] = helper[key];
    }

    for (let key in constant) {
        app.config.globalProperties[`${key}`] = constant[key];
    }

    if(injectKey.config.cache.useStore) {
        appCache.useStore(globalStore);
    }

    uni.Cache = appCache;
    uni.Http = appHttp;
    uni.Msg = msg;
    uni.Log = appLog;
    uni.Constant = constant;
    uni.Helper = helper;
}

export const useStore = () => {return globalStore()}
export {appCache as Cache, config as GlobalConfig, appHttp as Http, msg as Msg, appLog as Log, constant as Constant, helper as Helper};
